/**
    *@class functions
	*@description On load the startTime will start and it will run until  we close Thunderbird.
	*
    *@author Rusz Erzsebet
    *
    *@see startTime
    *@see MonthViewInit
    *@see InitWeekStart
	*@see DailyViewInit
*/

window.addEventListener("load", startTime , false);
window.addEventListener("load",MonthViewInit,false);
window.addEventListener("load",InitWeekStart,false);
window.addEventListener("load",DailyViewInit,false);
//window.addEventListener("load",initCheck,false);

/**
    *@class functions
	*@description It opens the New Category window.
	*
    *@author Rusz Erzsebet
*/

function newCategory() {
	window.openDialog("chrome://Calendar/content/XulFiles/newCategory.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
}

/**
    *@class functions
	*@description Will open the viewWindow.
	*
    *@author Rusz Erzsebet
*/

function viewWindow() {
	window.openDialog("chrome://Calendar/content/XulFiles/viewEventForm.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
} 
function openHelp(){
	window.openDialog("chrome://Calendar/content/Need help with the Calendar.htm", "_blank", "chrome,modal,centerscreen,resizable=yes, width=700,height=500");
}
/**
    *@class functions
	*@description  It will open the Modify Event Form.
	*
    *@author Rusz Erzsebet
*/

function modifyEventForm() {
	window.openDialog("chrome://Calendar/content/XulFiles/modifyEventForm.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
	
}
var MyRemindername = " ";
var MyReminderdescription = " ";
var MyRemindertime = " ";
var MyReminderdate = " ";
var MyRemindercategoryName = " ";
var MyReminderlocation = " ";

function checkEventForm(namee, descriptionn, timee, 
						datee, categoryNamee, locationn) {
	MyRemindername  = namee;
	MyReminderdescription = descriptionn;
	MyRemindertime = timee;
	MyReminderdate = datee;
	MyRemindercategoryName = categoryNamee;
	MyReminderlocation = locationn;
	alert(MyRemindercategoryName+"--"+MyReminderlocation+"--"+MyReminderdescription+"FORM");
	window.openDialog("chrome://Calendar/content/XulFiles/checkEvent.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
}

/**
    *@class functions
	*@description  It will open the start window (Callendar).
	*
    *@author Rusz Erzsebet
*/
 
function openWindow() {
	window.openDialog("chrome://Calendar/content/XulFiles/calendar1.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
}
 
/**
    *@class functions
	*@description   It will open the New Event Form.
	*
    *@author Rusz Erzsebet
*/

function newEventForm() {
	window.openDialog("chrome://Calendar/content/XulFiles/newEventForm.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");		
}

/**
    *@class functions
	*@description   It will open the Delete Event Form.
	*
    *@author Rusz Erzsebet
*/

function deleteEventForm() {
	window.openDialog("chrome://Calendar/content/XulFiles/deleteEventForm.xul", "_blank", "chrome,modal,centerscreen,resizable=yes");
}

/**
    *@class functions
	*@description  The perfect time will appear.
	*
    *@author Rusz Erzsebet
*/

function startTime() {
	var today=new Date();
	document.getElementById('txt').label=today.toString();
	t=setTimeout('startTime()',500);
}

/**
    *@class functions
	*@description  In case we cancel an event, the cells value will be emty.
	*
    *@author Rusz Erzsebet
*/

function cancelEvent() {
	document.getElementById('title').value="";
	document.getElementById('location').value="";
	document.getElementById('description').value="";
}

/**
    *@class functions
	*@description  We have to decide if we pressed the left or the right button.
	*
    *@author Rusz Erzsebet
*/

function dontes(e) {
	if (e.button == 2) 
		modifyEventForm();
		else
		 if (e.button == 0) {
			 newEventForm();
		 }
}
function splitDescription(){
		var words = [];
	words = MyReminderdescription.split(" ");
	var description2="";
	var  i = 0;
	var max = 20;
	var sum = 0;
	while (i<words.length) {
		if (sum+words[i].length<=max) {
			MyReminderdescription2 = description2.concat(words[i]," ");
			sum = sum + words[i].length;
		}
		else {
			description2 = description2.concat("\n",words[i]," ");
			sum = words[i].length;
		}
		i++;
	}
	return new String(description2);
}
/**
	*@class functions
	*@description  This function will handle the new category.
	*
    *@author Rusz Erzsebet
	
*/
function newCategoryForm(categoryName){
	if (getCategoryIndex(categoryName) != -1){
		alert("The given category exits in the database!");
		return;
	}
	if (categoryName.length == 0)
		alert("Please write a category name!");
	else {
		if (ok == -1)
			return;	
		try{
			newCategory(categoryName);
			alert("You have a new category: "+categoryName+"!");
			window.close('insertCategoryForm');
		}catch(err){
			return;
		}
	}	
}
function initCheck(){
	var rootDate = document.getElementById('checkDate');
	var rootTitle = document.getElementById('checkTitle');
	var rootTime = document.getElementById('checkTime');	
	var rootCat = document.getElementById('checkCat');		
	var rootLocation = document.getElementById('checkLocation');
	var rootDescription = document.getElementById('checkDescription');
	alert(MyRemindername+"--"+MyReminderdate+"--"+MyRemindertime+"--"+
	MyRemindercategoryName+"--"+MyReminderlocation+"--"+MyReminderdescription);
	rootDate.setAttribute("value",MyReminderdate);
	rootTitle.setAttribute("value",MyRemindername);
	rootTime.setAttribute("value",MyRemindertime);
	rootCat.setAttribute("value",MyRemindercategoryName);
	rootLocation.setAttribute("value",MyReminderlocation);
	rootDescription.setAttribute("value",splitDescription());
}
/*Convert from date to day (int)*/

/**
    *@class functions
	*@description  Convert from date to day (int).
	*
    *@author Resz Janos
    *@param date The parameter is a date
    *@return The function will return the day of the date
*/

function dateToDay(date) {
	var datee = new Date(date);
	return new Number(datee.getDate());
}

/**
    *@class functions
	*@description  Convert from date to month(int).
	*
    *@author Resz Janos
    *@param date The parameter is a date
    *@return The function will return the month of the date
*/

function dateToMonth(date) {
	var datee = new Date(date);
	return new Number(datee.getMonth()+1);
}

/**
    *@class functions
	*@description  Convert from date to year(int).
	*
    *@author Resz Janos
    *@param date The parameter is a date
    *@return The function will return the year of the date
*/

function dateToYear(date) {
	var datee = new Date(date);
	return new Number(datee.getFullYear());
}

/**
    *@class functions
	*@description  Convert year, month, day ints to date(String).
	*
    *@author Resz Janos
    *@param year
	*@param month
	*@param day
    *@return The function will return the string date
*/

function yearMonthDayToDate(year, month, day) {
	return new String(year+"-"+month+"-"+day);
}

/**
    *@class functions
	*@description Convert hour, minute ints to time(String).
	*
    *@author Resz Janos
    *@param hourr
	*@param minutee
    *@return The function will return the string time
*/

function hourMinuteToTime(hourr, minutee) {
	var hour = new Number(hourr);
	var minute = new Number(minutee);
	if (minute.valueOf() / 10 < 1) {
		var time = new String(hour.valueOf()+":0"+minute.valueOf());
		return time;
	}
	var time = new String(hour.valueOf()+":"+minute.valueOf());
	return time;
}

/**
    *@class functions
	*@description The function will get the description of the event by date, time, category and title
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
	*@param category
	*@param title
    *@return description
*/

function getTheDescriptionByDateTimeCategoryTitle(date, time, category, title){
	var description = getDescriptionByYearDayMonthTitleTime(dateToYear(date), dateToMonth(date), dateToDay(date), 
							  title, category, timeToHour(time), timeToMinute(time));
	return description;
}

/**
    *@class functions
	*@description The function will get the location of the event by date, time, category and title
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
	*@param category
	*@param title
    *@return location
*/

function getTheLocationByDateTimeCategoryTitle(date, time, category, title){
	var location = getLocationByYearDayMonthTitleTime(dateToYear(date), dateToMonth(date), dateToDay(date), 
							  title, category, timeToHour(time), timeToMinute(time));
	return location;
}

/**
    *@class functions
	*@description The function will load all the titles by date
	*
    *@author Rusz Erzsebet
    *@param _date
	*@see getTimeFromEventView
	*@see setCategoryView
	*@see setLocationView
	*@see setDescriptionView
*/

function getTitleFromDayView(_date) {
	var title = [];
	title = getEventTitleByDate(dateToYear(_date),dateToMonth(_date),dateToDay(_date));
	var root = document.getElementById('listTitleView');	
	var root2 = document.getElementById('timeView');
	var root3 = document.getElementById('theCat');
	var root4 = document.getElementById('theLocation');
	var description2="";
	document.getElementById('theDescription').value = description2 ;
	/* First remove everything, prepare it for the new events */
	root.removeAllItems();
	root.setAttribute("label","");
	root2.removeAllItems();
	root2.setAttribute("label","");
	root3.setAttribute("value","");
	root4.setAttribute("value","");	
	/*  The titles will be given */
    root.setAttribute("label",title[0].toString());
	getTimeFromEventView(_date,title[0]);
	setCategoryView(_date,title[0]);
	setLocationView(_date,title[0]);
	setDescriptionView(_date,title[0]);
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popView");
    for (var i = 0; i < title.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", title[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will set the category on given date and by title.
	*
    *@author Rusz Erzsebet
    *@param _date
	*@param title
	*@see getCategoryByDateTimeTitleView
*/

function setCategoryView(_date,title) {
	var time = document.getElementById('timeView').label;
	getCategoryByDateTimeTitleView(_date,time, title);
}

/**
    *@class functions
	*@description The function will set the time on given date and by title.
	*
    *@author Rusz Erzsebet
    *@param date
	*@param title
	*@see getLocationByDateTimeTitleView
*/

function setLocationView(date, title) {
	var time = document.getElementById('timeView').label;
	getLocationByDateTimeTitleView(date, time, title);
}

/**
    *@class functions
	*@description The function will set the time on given date and by title.
	*
    *@author Rusz Erzsebet
    *@param date
	*@param title
	*@see getDescriptionByDateTimeTitleView
*/

function setDescriptionView(date, title) {
	var time = document.getElementById('timeView').label;
	getDescriptionByDateTimeTitleView(date, time, title);
}

/**
    *@class functions
	*@description The function will load the times on given date and by title
	*
    *@author Rusz Erzsebet
    *@param _date
	*@param title
	*@see getCategoryByDateTimeTitleView
	*@see getDescriptionByDateTimeTitleView
	*@see getLocationByDateTimeTitleView
*/

function getTimeFromEventView(_date,title) {	
	var time = [];
	time = getEventTimeByDateTitle(dateToYear(_date),dateToMonth(_date),dateToDay(_date),title);
	var root = document.getElementById('timeView');	
	var root2 = document.getElementById('theCat');
	var root3 = document.getElementById('theLocation');
	var root4 = document.getElementById('theDescription');	
	/* We need to preapare all the elements for the new event that will come up */
	root.removeAllItems();
	root.setAttribute("label","");
	root2.setAttribute("value","");
	root3.setAttribute("value","");
	root4.setAttribute("value","");	
	/* We will insert the new times and according to the frist time the location and desxription */
    root.setAttribute("label",time[0].toString());
	getCategoryByDateTimeTitleView(_date,time[0].toString(), title);
	getDescriptionByDateTimeTitleView(_date,time[0].toString(), title);
	getLocationByDateTimeTitleView(_date,time[0].toString(), title);
	getReminderDateByDateTimeTitleView(_date,time[0].toString(), title);
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popTimeView");
    for (var i = 0; i<time.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", time[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will get the category by date, time, title
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
	*@param title
	*@see getCategoryByDateTimeTitle
*/

function getCategoryByDateTimeTitleView(date, time, title) {
	var category = getCategoryByDateTimeTitle(dateToYear(date),dateToMonth(date),dateToDay(date),timeToHour(time),timeToMinute(time),title);
	document.getElementById('theCat').value = category;
}
function getReminderDateByDateTimeTitleView(date, time, title) {
	var myDate = getReminderDateByYearDayMonthTitleTime(dateToYear(date),dateToMonth(date),dateToDay(date),timeToHour(time),timeToMinute(time),title);
	document.getElementById('theReminderDateTime').value = myDate;
}
/**
    *@class functions
	*@description The function will get the description by date, time, title
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
	*@param title
	*@see getDescriptionByYearDayMonthTitleTime
*/

function getDescriptionByDateTimeTitleView(date, time, title) { 
	var description = getDescriptionByYearDayMonthTitleTime(dateToYear(date),dateToMonth(date),
													dateToDay(date), title, 
													timeToHour(time),
													timeToMinute(time));
	var words = [];
	words = description.split(" ");
	var description2="";
	var  i = 0;
	var max = 20;
	var sum = 0;
	while (i<words.length) {
		if (sum+words[i].length<=max) {
			description2 = description2.concat(words[i]," ");
			sum = sum + words[i].length;
		}
		else {
			description2 = description2.concat("\n",words[i]," ");
			sum = words[i].length;
		}
		i++;
	}
	document.getElementById('theDescription').value = description2 ;
}

/**
    *@class functions
	*@description The function will get the location by date, time, title
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
	*@param title
	*@see getLocationByYearDayMonthTitleTime
*/

function getLocationByDateTimeTitleView(date, time, title) { 
	var location = getLocationByYearDayMonthTitleTime(dateToYear(date),dateToMonth(date),
													dateToDay(date), title, 
													timeToHour(time),
													timeToMinute(time));
	document.getElementById('theLocation').value = location;
}

/**
    *@class functions
	*@description The function will load all titles on given date on the Delete form
	*
    *@author Rusz Erzsebet
    *@param _date
	*@see getEventTitleByDate
	*@see getTimeFromEventDelete	
*/

function getTitleFromDayDelete(_date) {
	var title = [];
	title = getEventTitleByDate(dateToYear(_date),dateToMonth(_date),dateToDay(_date));
	var root = document.getElementById('list13');	
	var root2 = document.getElementById('timeDelete');
	
	root.removeAllItems();
	root.setAttribute("label","");
	root2.removeAllItems();
	root2.setAttribute("label","");
	
    root.setAttribute("label",title[0].toString());
	
	getTimeFromEventDelete(_date,title[0]);
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popDelete");
    for (var i = 0; i<title.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", title[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will load all times on given date and title on the Delete form
	*
    *@author Rusz Erzsebet
    *@param _date
	*@param title
	*@see getEventTimeByDateTitle
*/

function getTimeFromEventDelete(_date,title) {	
	var time = [];
	time = getEventTimeByDateTitle(dateToYear(_date),dateToMonth(_date),dateToDay(_date),title);
	var root = document.getElementById('timeDelete');	
	root.removeAllItems();
	root.setAttribute("label","");
    root.setAttribute("label",time[0].toString());
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popTimeDelete");
    for (var i = 0; i<time.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", time[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will load all titles on given date on the Modify form
	*
    *@author Rusz Erzsebet
    *@param _date
	*@see getEventTitleByDate
	*@see getTimeFromEventModify
	*@see setCategory
*/

function getTitleFromDayModify(_date) {
	var title = [];
	title = getEventTitleByDate(dateToYear(_date),dateToMonth(_date),dateToDay(_date));
	var root = document.getElementById('listTitleModify');	
	var root2 = document.getElementById('timeModify');
	var root3 = document.getElementById('oldCat');
	root.removeAllItems();
	root.setAttribute("label","");
	root2.removeAllItems();
	root2.setAttribute("label","");
	root3.setAttribute("value","");
    root.setAttribute("label",title[0].toString());
	getTimeFromEventModify(_date,title[0]);
	setCategory(_date,title[0]);
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popModify");
    for (var i = 0; i<title.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", title[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will set the category by date and title
	*
    *@author Rusz Erzsebet
    *@param _date
    *@param title
	*@see getCategoryByDateTimeTitleModify
*/

function setCategory(_date,title) {
	var time = document.getElementById('timeModify').label;
	getCategoryByDateTimeTitleModify(_date,time, title);
}

/**
    *@class functions
	*@description The function will load all times by date and title on the Modify form
	*
    *@author Rusz Erzsebet
    *@param _date
    *@param title
	*@see getCategoryByDateTimeTitleModify
	*@see getEventTimeByDateTitle
*/

function getTimeFromEventModify(_date,title) {	
	var time = [];
	time = getEventTimeByDateTitle(dateToYear(_date),dateToMonth(_date),dateToDay(_date),title);
	var root = document.getElementById('timeModify');	
	var root2 = document.getElementById('oldCat');
	root.removeAllItems();
	root.setAttribute("label","");
	root2.setAttribute("value","");
    root.setAttribute("label",time[0].toString());
	getCategoryByDateTimeTitleModify(_date,time[0].toString(), title);
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","popTimeModify");
    for (var i = 0; i<time.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", time[i].toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}

/**
    *@class functions
	*@description The function will get the category by date, time, title on the Modify form
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
    *@param title
	*@see getCategoryByDateTimeTitle
*/

function getCategoryByDateTimeTitleModify(date,time, title) {
	var category = getCategoryByDateTimeTitle(dateToYear(date),dateToMonth(date),dateToDay(date),timeToHour(time),timeToMinute(time),title);
	document.getElementById('oldCat').value = category;
}

/**
    *@class functions
	*@description The function will update the given event in the Modify form
	*
    *@author Rusz Erzsebet
    *@param oldDate
	*@param newDate
    *@param name
	*@param newName
	*@param time
	*@param newTime
	*@param categoryName
	*@param newCategoryName
	*@see modifyEvent
*/

function updateEvent( oldDate, newDate, name,newName, time, newTime, categoryName, newCategoryName) {
	/* If the new title is not given... an alert */
	if (name == "")
		alert("Please select an existing event!");
		else if (newName == "") {
		/* If there is no event to modify... an alert */
			alert("Please write the new title of the event!");
		}
		else {
		/* We modify the event and close the window */
			modifyEvent( dateToYear(oldDate),dateToMonth(oldDate),dateToDay(oldDate), dateToYear(newDate),dateToMonth(newDate),dateToDay(newDate),
							 name, newName, timeToHour(time),timeToMinute(time), timeToHour(newTime),timeToMinute(newTime), categoryName, newCategoryName);
			alert("The event is modified!");							 
			window.close('modifyEventForm');
			}
}

/**
    *@class functions
	*@description The function will delete the given event by date, time and title from the database.
	*
    *@author Rusz Erzsebet
    *@param date
	*@param time
    *@param title
	*@see deleteEvent
*/

function deleteEventFromDB(date, time, title) {
	if (title=="")
		alert("Please select an existing event!");
		else {
			deleteEvent(dateToYear(date),dateToMonth(date),dateToDay(date),timeToHour(time),timeToMinute(time),title);
			alert("Event deleted!");
			window.close('deleteEventForm');
		}
}

/**
    *@class functions
	*@description The function will get the titles by date.
	*
    *@author Rusz Erzsebet
    *@param _date
	*@see getEventTitleByDate
	*@see dateToYear
	*@see dateToMonth
	*@see dateToDay
*/

function getTitleFromDay(_date) {
	getEventTitleByDate(dateToYear(_date),dateToMonth(_date),dateToDay(_date));
}

/**
    *@class functions
	*@description A time from XUL convert the hour to int.
	*
    *@author Resz Janos
    *@param time
	 *@return new Number(hour)
*/

function timeToHour(time) {
	timee = new String(time);
	if (timee.length == 8){
		timee = timee.substr(0,5);
	}
	if (timee.length == 7){
		timee = timee.substr(0,4);
	}
	if (timee.length == 4){
		hour = timee.substr(0,1);
		return new Number(hour);
	}
	hour = timee.substr(0,2);
	return new Number(hour);
}

/**
    *@class functions
	*@description  A time from XUL convert the minute to int.
	*
    *@author Resz Janos
    *@param time
	 *@return new Number(minute)
*/

function timeToMinute(time) {
	timee = new String(time);
	if (timee.length == 8){
		timee = timee.substr(0,5);
	}
	if (timee.length == 7){
		timee = timee.substr(0,4);
		minute = timee.substr(2,2);
		return new Number(minute);
	}
	minute = timee.substr(3,2);
	return new Number(minute);
}

/**
    *@class functions
	*@description The function will store a new event
	*
    *@author Rusz Erzsebet
    *@param title
	*@param location
	*@param category
	*@param startDate
	*@param startTime
	*@param endDate
	*@param endTime
	*@param repeat
	*@param reminder
	*@param description
	*@see newEvent
	*@see timeToHour
	*@see timeToMinute
	*@see dateToYear
	*@see dateToMonth
	*@see dateToDay
	*@see dateToYear
	*@see dateToMonth
	*@see dateToDay
	*@see timeToHour
	*@see timeToMinute		
*/

function insertNewEvent(title, location, category, startDate, startTime, endDate,
						endTime, repeat, reminder, description) {
	if (title.length == 0){
		alert("You must write a title!");
		return;
	}
	if (location.length == 0) {
		var theLocation = new String("-");
	}
	else {
		var theLocation = new String(location.valueOf());
	}
	if (description.length == 0) {
		var theDescription = new String("-");
	}else {
		var theDescription = new String(description.valueOf());
	}
	var stored = newEvent(category, reminder, repeat,
					timeToHour(startTime), timeToMinute(startTime), 
					title, theLocation, theDescription,
					dateToYear(startDate), dateToMonth(startDate),
					dateToDay(startDate), dateToYear(endDate),
					dateToMonth(endDate), dateToDay(endDate),
					timeToHour(endTime), timeToMinute(endTime));				
	if (stored == 0) {
		alert("The event is stored!");
		window.close('newEeventForm');
	}
}

var D_year;
var D_month;
var D_day;
var todayYear;
var todayMonth;
var todayDay;

/**
    *@class functions
	*@description The function will clear the cells
	*
    *@author Varga Anita
*/

function deleteFromHours() {
	for (var i=0; i<24; i++)
		document.getElementById("hour"+i).setAttribute("label","");
}

/**
    *@class functions
	*@description The function will initialize the Daily View
	*
    *@author Varga Anita
	*@param D_year
	*@param D_month
	*@param D_day
	*@param todayYear
	*@param todayMonth
	*@param todayDay	
	*@see itsEventAtYearMonthDayHour
	*@see deleteFromHours
*/

function DailyViewInit(){
	var date = new Date();
	D_year = date.getFullYear();
	D_month = date.getMonth()+1;
	D_day = date.getDate();
	todayYear = date.getFullYear();
	todayMonth = date.getMonth()+1;
	todayDay = date.getDate();
	var month_name = "";
	if (D_month == 1)  { month_name = "January";}
	if (D_month == 2)  { month_name = "February";}
	if (D_month == 3)  { month_name = "March";}	
	if (D_month == 4)  { month_name = "April";}	
	if (D_month == 5)  { month_name = "May";}	
	if (D_month == 6)  { month_name = "Jun";}	
	if (D_month == 7)  { month_name = "July";}	
	if (D_month == 8)  { month_name = "August";}	
	if (D_month == 9)  { month_name = "September";}	
	if (D_month== 10)  { month_name = "October";}	
	if (D_month == 11)  { month_name = "November";}	
	if (D_month == 12)  { month_name = "December";}		
	document.getElementById("monthvalue2").value = month_name;
	document.getElementById("yearvalue2").value = D_year;
	document.getElementById("dayvalue2").value = D_day;
	/* sets the events on given date and hour, giving the minutes that the event will start and the titles of the event */
	var result;
	deleteFromHours();
	for (var j = 0; j<24; j++) {
		var last = "";
		var butt = document.getElementById("hour"+j);
		last = butt.value;
		 result =  itsEventAtYearMonthDayHour(D_year, D_month, D_day, j); 
		  if (result.length == 0) {butt.value="";} 
		  else {
			for (var i = 0; i<result.length; i++) {
				last = last +"   "+result[i].minute+": "+result[i].name;
			}
			butt.setAttribute("label",last);
		}
	}
	if ((todayDay == D_day) && (todayMonth == D_month) && (todayYear == D_year)) {
				document.getElementById("dailyvalue").style.color='#000000';
		}
		else {
			document.getElementById("dailyvalue").style.color='#FF0000';
		}
	var d = new Date(D_year,D_month-1,D_day);
    var day_num = d.getDay();	
	var day_name = "";
	if (day_num == 0) { day_name = "Sunday";}
	if (day_num == 1) { day_name = "Monday";}
	if (day_num == 2) { day_name = "Tuesday";}
	if (day_num == 3) { day_name = "Wednesday";}
	if (day_num == 4) { day_name = "Thursday";}
	if (day_num == 5) { day_name = "Friday";}
	if (day_num == 6) { day_name = "Saturday";}
	document.getElementById("dailyvalue").value = day_name;
}

/**
    *@class functions
	*@description The function will load on the right button
	*
    *@author Varga Anita
	*@param D_year
	*@param D_month
	*@param D_day
	*@see itsEventAtYearMonthDayHour
	*@see deleteFromHours
*/

function DailyViewNext(){
	var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (D_month == 2) {
	   Days_in_Month[1] = ((D_year % 400 == 0) || ((D_year % 4 == 0) && (D_year % 100 !=0))) ? 29 : 28; //szokoev ellenorzes
    }
   deleteFromHours();
    D_day = D_day + 1;
    if ( D_day == Days_in_Month[D_month-1]+1) {
			D_day = 1;
			D_month = D_month + 1;
			if (D_month > 12) {
				D_year = D_year+1;
				D_month = 1;
			}		
			var month_name = "";
			if (D_month == 1)  { month_name = "January";}
			if (D_month == 2)  { month_name = "February";}
			if (D_month == 3)  { month_name = "March";}	
			if (D_month == 4)  { month_name = "April";}	
			if (D_month == 5)  { month_name = "May";}	
			if (D_month == 6)  { month_name = "Jun";}	
			if (D_month == 7)  { month_name = "July";}	
			if (D_month == 8)  { month_name = "August";}	
			if (D_month == 9)  { month_name = "September";}	
			if (D_month== 10)  { month_name = "October";}	
			if (D_month == 11)  { month_name = "November";}	
			if (D_month == 12)  { month_name = "December";}		
			document.getElementById("monthvalue2").value = month_name;
			document.getElementById("yearvalue2").value = D_year;
			document.getElementById("dayvalue2").value = D_day;
	}
	else 
	{
		var result;
			for (var j = 0; j<24; j++) {
				var last = "";
				var butt = document.getElementById("hour"+j);
				last = butt.value;
				 result =  itsEventAtYearMonthDayHour(D_year, D_month, D_day, j); 
				 if (result.length == 0) {butt.value="";} 
				 else {
					for (var i = 0; i<result.length; i++) {
						last = last +"   "+result[i].minute+": "+result[i].name;
					}
					butt.setAttribute("label",last);
				}
			}
	}
		if ((todayDay == D_day) && (todayMonth == D_month) && (todayYear == D_year)) {
				document.getElementById("dailyvalue").style.color='#000000';
		}
		else {
			document.getElementById("dailyvalue").style.color='#FF0000';
		}
		var d = new Date(D_year,D_month-1,D_day);
		day_num = d.getDay();	/* The day on the given week */
		var day_name = "";
		if (day_num == 0) { day_name = "Sunday";}
		if (day_num == 1) { day_name = "Monday";}
		if (day_num == 2) { day_name = "Tuesday";}
		if (day_num == 3) { day_name = "Wednesday";}
		if (day_num == 4) { day_name = "Thursday";}
		if (day_num == 5) { day_name = "Friday";}
		if (day_num == 6) { day_name = "Saturday";}
		document.getElementById("dailyvalue").value = day_name;
		document.getElementById("dayvalue2").value = D_day;
}

/**
    *@class functions
	*@description The function will load on the left button
	*
    *@author Varga Anita
	*@param D_year
	*@param D_month
	*@param D_day
	*@see itsEventAtYearMonthDayHour
	*@see deleteFromHours
*/

function  DailyViewPrevious() {
	var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
     D_day = D_day - 1;
    if ( D_day < 1) {
			D_month = D_month - 1;
			if (D_month == 2) 
				Days_in_Month[1] = ((D_year % 400 == 0) || ((D_year % 4 == 0) && (D_year % 100 !=0))) ? 29 : 28;  D_day = Days_in_Month[D_month-1];
			D_day = Days_in_Month[D_month-1];
			if (D_month < 1) {
				D_year = D_year-1;
				D_month = 12;
				D_day = Days_in_Month[D_month-1];
			}		
			var month_name = "";
			if (D_month == 1)  { month_name = "January";}
			if (D_month == 2)  { month_name = "February";}
			if (D_month== 3)  { month_name = "March";}	
			if (D_month == 4)  { month_name = "April";}	
			if (D_month == 5)  { month_name = "May";}	
			if (D_month == 6)  { month_name = "Jun";}	
			if (D_month == 7)  { month_name = "July";}	
			if (D_month == 8)  { month_name = "August";}	
			if (D_month == 9)  { month_name = "September";}	
			if (D_month== 10)  { month_name = "October";}	
			if (D_month == 11)  { month_name = "November";}	
			if (D_month == 12)  { month_name = "December";}		
			document.getElementById("monthvalue2").value = month_name;
			document.getElementById("yearvalue2").value = D_year;
			document.getElementById("dayvalue2").value = D_day;
	}
	else 
	{
		/* sets the events on given date and hour, giving the minutes that the event will start and the titles of the event */
			deleteFromHours();
			var result;
			for (var j = 0; j<24; j++) {
				var last = "";
				var butt = document.getElementById("hour"+j);
				last = butt.value;
				 result =  itsEventAtYearMonthDayHour(D_year, D_month, D_day, j); 
				  if (result.length == 0) {butt.value= "";} 
				  else {
					for (var i = 0; i<result.length; i++) {
						last = last +"   "+result[i].minute+": "+result[i].name;
					}
					butt.setAttribute("label",last);
				}
			}
	}
		if ((todayDay == D_day) && (todayMonth == D_month) && (todayYear == D_year)) {
				document.getElementById("dailyvalue").style.color='#000000';
		}
		else {
			document.getElementById("dailyvalue").style.color='#FF0000';
		}
		var d = new Date(D_year,D_month-1,D_day);
		day_num = d.getDay();	/* The day on the given week */
		var day_name = "";
		if (day_num == 0) { day_name = "Sunday";}
		if (day_num == 1) { day_name = "Monday";}
		if (day_num == 2) { day_name = "Tuesday";}
		if (day_num == 3) { day_name = "Wednesday";}
		if (day_num == 4) { day_name = "Thursday";}
		if (day_num == 5) { day_name = "Friday";}
		if (day_num == 6) { day_name = "Saturday";}
		document.getElementById("dailyvalue").value = day_name;
		document.getElementById("dayvalue2").value = D_day;
}

/**
    *@class functions
	*@description The function will load the Daily view
	*
    *@author Varga Anita
	*@see  DailyViewPrevious
	*@see  DailyViewNext
*/

function Daily() {
	var div0 = document.getElementById('divMain0');
	var rows = document.createElement('rows');
	var row = document.createElement('row');
	var button = document.createElement('button');
	button.className="style";		
	button.setAttribute("image" ,"chrome://Calendar/content/Images/left.png" );
	button.onclick = DailyViewPrevious;
	row.appendChild(button);
	var label = document.createElement('label');
	label.className = "dates1";
	label.setAttribute("id", "yearvalue2");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates1";
	label.setAttribute("id", "monthvalue2");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates1";
	label.setAttribute("id", "dayvalue2");
	row.appendChild(label);	
	var label = document.createElement('label');
	label.className = "dates";
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates1";
	row.appendChild(label);	
	var label = document.createElement('label');
	label.className = "dates";
	label.setAttribute("id", "dailyvalue");
	row.appendChild(label);
	var button = document.createElement('button');
	button.className="style";		
	button.setAttribute("image" ,"chrome://Calendar/content/Images/right.png" );
	button.onclick = DailyViewNext;
	row.appendChild(button);	
	rows.appendChild(row);
	div0.appendChild(rows);
	var div = document.getElementById('divMain1');		
	var grid = document.createElement('grid');
	grid.className="scrollable";
	var rows = document.createElement('rows');
	var row = document.createElement('row');
	var columns = document.createElement('columns');	
	for (var i = 0;i<2;i++) {
		var column = document.createElement('column');
		columns.appendChild(column);
	}
	grid.appendChild(columns);
	for (var j = 0; j< 24; j++) {
		var row = document.createElement('row');
        var button = document.createElement('button');
		button.className="buttonsetup";		
		button.setAttribute("label", j + ":00");
		row.appendChild(button);	
		var button = document.createElement('button');
		button.className="buttonsetup1";
		button.value= "";
		button.onclick = dontes;
		button.setAttribute("id","hour"+j);
		row.appendChild(button);
		rows.appendChild(row);
    }
		grid.appendChild(rows);
		div.appendChild(grid);
}

var dStart;
var dEnd;
var W_year;
var W_month;
var W_day;
var DaysinMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var todayDay;
var todayMonth;
var todayYear;

/**
    *@class functions
	*@description The function will initialize the Daily View
	*
    *@author Varga Anita
	*@author Rusz Erzsebet
	*@param W_year
	*@param W_month
	*@param W_day
	*@param todayYear
	*@param todayMonth
	*@param todayDay	
	*@see  howManyYearMonthDayHour
*/

function InitWeekStart() {
	dStart = new Date();
	W_year = dStart.getFullYear();
	W_month = dStart.getMonth()+1;
	W_day = dStart.getDate();
	todayYear = dStart.getFullYear();
	todayMonth = dStart.getMonth()+1;
	todayDay = dStart.getDate();
	if (W_month == 2) {
	   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28; 
   }	
	var h = dStart.getDay();
	while (h != 0) {
		W_day = W_day - 1;
		if (W_day < 1) {
			W_day = DaysinMonth[W_month-2];  
			W_month = W_month-1;
			if (W_month == 2) {
				   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28;
		   }
			if (W_month < 1) {
				W_month = 12;
				W_year = W_year-1;
			}
		}		
		var nr,sum;	
		for (var i=0; i<24; i++) {
			nr = howManyYearMonthDayHour(W_year, W_month, W_day, i);
			if (nr != 0) {
				sum = i*7+1;
				document.getElementById(sum + "week").setAttribute("label",nr);
			}
			else {
				sum = i*7+1;
			document.getElementById(sum + "week").setAttribute("label","");
			}
		}		
		dStart = new Date(W_year,W_month-1,W_day);
		h = dStart.getDay();
	}
	document.getElementById("WeekLabelStart").value = W_year+"-"+W_month+"-"+W_day+"  to ";
	SetWeekEndPlus();
}

/**
    *@class functions
	*@description The function will load the Weekly view when next is clicked
	*
    *@author Varga Anita
	*@author Rusz Erzsebet
	*@param W_year
	*@param W_month
	*@param W_day
	*@see  howManyYearMonthDayHour
	*@see SetWeekEndPlus
*/

function SetWeekStartPlus() {
	W_year = dEnd.getFullYear();
	W_month = dEnd.getMonth()+1;
	W_day = dEnd.getDate();
	W_day = W_day +1;
	if (W_day > DaysinMonth[W_month-1]) {
		W_day = 1;
		W_month = W_month+1;
		if (W_month == 2) {
			   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28; //szokoev ellenorzes
		   }
		if (W_month > 12) {
			W_month = 1;
			W_year = W_year+1;
			W_day = 1;
		}
	}
	var nr,sum; 
	for (var i=0; i<24; i++) {
		nr = howManyYearMonthDayHour(W_year, W_month, W_day, i);
		if (nr != 0) {
				sum = i*7+1;
				document.getElementById(sum + "week").setAttribute("label",nr);
		}
		else {
				sum = i*7+1;
				document.getElementById(sum + "week").setAttribute("label","");
		}
	}
	
	dStart = new Date(W_year,W_month-1,W_day);
	document.getElementById("WeekLabelStart").value = W_year+"-"+W_month+"-"+W_day+"  to ";
	SetWeekEndPlus();
}

/**
    *@class functions
	*@description The function will load the Weekly view when next is clicked
	*
    *@author Varga Anita
	*@author Rusz Erzsebet
	*@param W_year
	*@param W_month
	*@param W_day
    *@param todayYear
	*@param todayMonth
	*@param todayDay
	*@see  howManyYearMonthDayHour
*/

function SetWeekEndPlus() {
	var no = 0;
	var where = 1;
	var values = "";
	values= document.getElementById("Day"+where).value;
	values = values.substr(0,3);
	if ((todayMonth == W_month) && (todayYear == W_year) && (todayDay == W_day)) {
			document.getElementById("Day"+where).style.color='#000000';
	}
	else {
		document.getElementById("Day"+where).style.color='#FF0000';
	}
	document.getElementById("Day"+where).value = values+"  "+W_day;
	while (no < 6) {
		no=no+1;
		W_day = W_day +1;
		if (W_day > DaysinMonth[W_month-1]) {
			W_day = 1;
			W_month = W_month+1;
			if (W_month == 2) {
				   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28; //szokoev ellenorzes
			   }
			if (W_month > 12) {
				W_month = 1;
				W_year = W_year+1;
				W_day = 1;
			}
		}
		where = where+1;
		var nr,sum; 
		for (var i=0; i<24; i++) {
			nr = howManyYearMonthDayHour(W_year, W_month, W_day, i);
			if (nr != 0) {
					sum = i*7+where;
					document.getElementById(sum + "week").setAttribute("label",nr);
			}
			else {
				sum = i*7+where;
				document.getElementById(sum + "week").setAttribute("label","");
			}
		}
		
		values= document.getElementById("Day"+where).value;
		values = values.substr(0,3);
		if ((todayMonth == W_month) && (todayYear == W_year) && (todayDay == W_day)) {
			document.getElementById("Day"+where).style.color='#000000';
		}
		else {
			document.getElementById("Day"+where).style.color='#FF0000';
		}
		document.getElementById("Day"+where).value = values+"  "+W_day;
	}
	document.getElementById("WeekLabelEnd").value = W_year+"-"+W_month+"-"+W_day;
	dEnd = new Date(W_year, W_month-1, W_day);
}

/**
    *@class functions
	*@description The function will load the Weekly view when previous is clicked
	*
    *@author Varga Anita
	*@author Rusz Erzsebet
	*@param W_year
	*@param W_month
	*@param W_day
    *@param todayYear
	*@param todayMonth
	*@param todayDay
	*@see  howManyYearMonthDayHour
	*@see SetWeekEndMinus
*/

function SetWeekStartMinus() {
	W_year = dStart.getFullYear();
	W_month = dStart.getMonth()+1;
	W_day = dStart.getDate();	
	W_day = W_day - 1;
	if (W_day < 1) {
		W_day = DaysinMonth[W_month-2]; 
		W_month = W_month-1;
		if (W_month == 2) {
			   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28; 
		}
		if (W_month < 1) {
			W_month = 12;
			W_year = W_year-1;
			W_day =DaysinMonth[W_month-1];
		}
	}
	var nr,sum; 
	for (var i=0; i<24; i++) {
		nr = howManyYearMonthDayHour(W_year, W_month, W_day, i);
		if (nr != 0) {
			sum = i*7+1;
			document.getElementById(sum + "week").setAttribute("label",nr);
		}
		else {
			sum = i*7+1;
			document.getElementById(sum + "week").setAttribute("label","");
		}
	}
	dEnd = new Date(W_year, W_month-1, W_day);
	document.getElementById("WeekLabelEnd").value = W_year+"-"+W_month+"-"+W_day;
	SetWeekEndMinus();
}

/**
    *@class functions
	*@description The function will load the Weekly view when previous is clicked
	*
    *@author Varga Anita
	*@author Rusz Erzsebet
	*@param W_year
	*@param W_month
	*@param W_day
    *@param todayYear
	*@param todayMonth
	*@param todayDay
	*@see  howManyYearMonthDayHour
*/

function SetWeekEndMinus() {
	var no = 0;
	where = 7;
	var values="";
	values= document.getElementById("Day"+where).value;
	values = values.substr(0,3);
	if ((todayMonth == W_month) && (todayYear == W_year) && (todayDay == W_day)) {
			document.getElementById("Day"+where).style.color='#000000';
	}
	else {
		document.getElementById("Day"+where).style.color='#FF0000';
	}
	document.getElementById("Day"+where).value = values+"  "+W_day;
	while (no < 6) {
		no=no+1;
		W_day = W_day -1;
		if (W_day < 1) {
			W_day = DaysinMonth[W_month-2];
			W_month = W_month-1;
			if (W_month == 2) {
					   DaysinMonth[1] = ((W_year % 400 == 0) || ((W_year % 4 == 0) && (W_year % 100 !=0))) ? 29 : 28; //szokoev ellenorzes
				 }
			if (W_month < 1) {
				W_month = 12;
				W_year = W_year-1;
				W_day = DaysinMonth[W_month-1];
			}
		}
		where = where-1;
		var nr,sum; 
		for (var i=0; i<24; i++) {
			nr = howManyYearMonthDayHour(W_year, W_month, W_day, i);
			if (nr != 0) {
					sum = i*7+where;
					document.getElementById(sum + "week").setAttribute("label",nr);
			}
			else {
				sum = i*7+where;
				document.getElementById(sum + "week").setAttribute("label","");
			}
		}
    	values= document.getElementById("Day"+where).value;
		values = values.substr(0,3);
		if ((todayMonth == W_month) && (todayYear == W_year) && (todayDay == W_day)) {
			document.getElementById("Day"+where).style.color='#000000';
		}
		else {
			document.getElementById("Day"+where).style.color='#FF0000';
		}
		document.getElementById("Day"+where).value = values+"  "+W_day;
	}
	document.getElementById("WeekLabelStart").value = W_year+"-"+W_month+"-"+W_day+"  to ";
	dStart = new Date(W_year, W_month-1, W_day);
}

/**
    *@class functions
	*@description The function will load the Weekly view
	*
    *@author Varga Anita
	*@see SetWeekStartMinus 
	*@see SetWeekStartPlus
*/

function Weekly() {
	var div0 = document.getElementById('divMain00');
	var rows = document.createElement('rows');
	var row = document.createElement('row');	
	var button = document.createElement('button');
	button.className="buttonsetup000"; 
    button.onclick = SetWeekStartMinus;
	button.setAttribute("image" ,"chrome://Calendar/content/Images/left.png" );
	row.appendChild(button);	
	var label = document.createElement('label');
	label.className = "dates";
	row.appendChild(label);	
	var label = document.createElement('label');
	label.className = "dates";
	label.setAttribute("id", "WeekLabelStart");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates";
	label.setAttribute("id", "WeekLabelEnd");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates";
	row.appendChild(label);	
	var button = document.createElement('button');
	button.className="buttonsetup000";
	button.setAttribute("image" ,"chrome://Calendar/content/Images/right.png" );
	button.onclick = SetWeekStartPlus;
	row.appendChild(button);
	rows.appendChild(row);
	var row = document.createElement('row');
	var label = document.createElement('label');
	label.setAttribute("value", "Day : ");
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Sun");
	label.setAttribute("id", "Day"+1);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Mon");
	label.setAttribute("id", "Day"+2);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Tue");
	label.setAttribute("id", "Day"+3);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Wed");
	label.setAttribute("id", "Day"+4);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Thu");
	label.setAttribute("id", "Day"+5);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Fri");
	label.setAttribute("id", "Day"+6);
	label.className = "labelsetup";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Sat");
	label.setAttribute("id", "Day"+7);
	label.className = "labelsetup";
	row.appendChild(label);
	rows.appendChild(row);
	div0.appendChild(rows);
	var root = document.getElementById('divMain');		
	var grid = document.createElement('grid');
	grid.className="scrollable";
	var rows = document.createElement('rows');
	var row = document.createElement('row');
	var columns = document.createElement('columns');	
	for (var i = 0;i<9;i++) {
		var column = document.createElement('column');
		columns.appendChild(column);
	}
	grid.appendChild(columns);
	var it = 0;
	for (var j = 0; j< 24; j++) {
		var row = document.createElement('row');
        var button = document.createElement('button');
		button.className="buttonsetup";		
		button.setAttribute("label", j + ":00");
		row.appendChild(button);	
		for (var z = 0;z<7;z++) {	
			it = it+1;
			var button = document.createElement('button');
			button.className="buttonsetup";		
			button.setAttribute("id",it + "week");
			button.setAttribute("label","");
			button.onclick = dontes;
			row.appendChild(button);
		}
		rows.appendChild(row);
    }
		grid.appendChild(rows);
		root.appendChild(grid);
}

var M_year;
var M_month;
var M_day;
var Today_year;
var Today_month;
var Today_day;

/**
    *@class functions
	*@description The function will initialize the month view
	*
    *@author Varga Anita
	*@param M_year
	*@param M_month
	*@param M_day
    *@param todayYear
	*@param todayMonth
	*@param todayDay
	*@see InitionMonthDay
*/

function MonthViewInit(){
	var date = new Date();
	M_year = date.getFullYear();
	M_month = date.getMonth()+1;
	M_day = date.getDate();
	Today_year = M_year;
	Today_month = M_month;
	Today_day = M_day;	
	var gettingDate = new Date(Today_year,Today_month-1,1);
	var h =  gettingDate.getDay(); // a het melyik napja
	var it = h+1;
	while (it-h < Today_day) {   //vegigjarjuk a honap minden napjat
		it = it + 1;
	}
	today = document.getElementById(it);
	today.setAttribute("image","chrome://Calendar/content/Images/today.png");
	var month_name="";
	if (M_month == 1)  { month_name = "January";}
	if (M_month == 2)  { month_name = "February";}
	if (M_month == 3)  { month_name = "March";}	
	if (M_month == 4)  { month_name = "April";}	
	if (M_month == 5)  { month_name = "May";}	
	if (M_month == 6)  { month_name = "Jun";}	
	if (M_month == 7)  { month_name = "July";}	
	if (M_month == 8)  { month_name = "August";}	
	if (M_month == 9)  { month_name = "September";}	
	if (M_month== 10)  { month_name = "October";}	
	if (M_month == 11)  { month_name = "November";}	
	if (M_month == 12)  { month_name = "December";}		
	document.getElementById("monthvalue").value=month_name;
	document.getElementById("yearvalue").value=M_year;
	InitionMonthDay();
}

/**
    *@class functions
	*@description The function will initialize the month view on add
	*
    *@author Varga Anita
	*@param M_year
	*@param M_month
	*@param M_day
	*@see DeleteCells
	*@see InitionMonthDay
*/

function MonthViewAdd(){
	M_month = M_month+1;
	if (M_month>12) {
		M_year = M_year+1;
		M_month = 1;
	}
	var month_name="";
	if (M_month == 1)  { month_name = "January";}
	if (M_month == 2)  { month_name = "February";}
	if (M_month == 3)  { month_name = "March";}	
	if (M_month == 4)  { month_name = "April";}	
	if (M_month == 5)  { month_name = "May";}	
	if (M_month == 6)  { month_name = "Jun";}	
	if (M_month == 7)  { month_name = "July";}	
	if (M_month == 8)  { month_name = "August";}	
	if (M_month == 9)  { month_name = "September";}	
	if (M_month== 10)  { month_name = "October";}	
	if (M_month == 11)  { month_name = "November";}	
	if (M_month == 12)  { month_name = "December";}		
	document.getElementById("monthvalue").value=month_name;
	document.getElementById("yearvalue").value=M_year;
	DeleteCells();
	InitionMonthDay();
}


/**
    *@class functions
	*@description The function will initialize the month view on previous
	*
    *@author Varga Anita
	*@param M_year
	*@param M_month
	*@see DeleteCells
	*@see InitionMonthDay
*/

function MonthViewExtract(){
	M_month = M_month-1;
	if (M_month<1) {
		M_year = M_year-1;
		M_month = 12;
	}
	var month_name="";
	if (M_month == 1)  { month_name = "January";}
	if (M_month == 2)  { month_name = "February";}
	if (M_month == 3)  { month_name = "March";}	
	if (M_month == 4)  { month_name = "April";}	
	if (M_month == 5)  { month_name = "May";}	
	if (M_month == 6)  { month_name = "Jun";}	
	if (M_month == 7)  { month_name = "July";}	
	if (M_month == 8)  { month_name = "August";}	
	if (M_month == 9)  { month_name = "September";}	
	if (M_month== 10)  { month_name = "October";}	
	if (M_month == 11)  { month_name = "November";}	
	if (M_month == 12)  { month_name = "December";}		
	document.getElementById("monthvalue").value=month_name;
	document.getElementById("yearvalue").value=M_year;
	DeleteCells();
	InitionMonthDay();
}


/**
    *@class functions
	*@description The function will delete the cells
	*
    *@author Varga Anita
*/

function DeleteCells() { 
	var empty = ""; 
	for (var j = 1; j<= 38; j++) {
		document.getElementById(j).label = empty;
		document.getElementById(j).setAttribute("image","");
	}
}

/**
    *@class functions
	*@description The function will initialize the month view
	*
    *@author Varga Anita
	*@param M_year
	*@param M_month
	*@see itsEventAtYearMonthDay
*/

function InitionMonthDay() {
	var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (M_month == 2) {
	   Days_in_Month[1] = ((M_year % 400 == 0) || ((M_year % 4 == 0) && (M_year % 100 !=0))) ? 29 : 28; 
   }
    var day = 1;
	var d = new Date(M_year,M_month-1,day);
	var h =  d.getDay(); 
	var it = h+1;
	while (day <= Days_in_Month[M_month-1]) {   
		document.getElementById(it).label = day;
		if (itsEventAtYearMonthDay(M_year, M_month, day) == 1) {
			today = document.getElementById(it);
			today.style.color='blue';
		}
		else {
				today = document.getElementById(it);
				today.style.color='red';
				}
		day = day +1 ;
		it = it + 1;
	}
	var gettingDate = new Date(Today_year,Today_month-1,1);
	var z =  gettingDate.getDay(); 
	var it = z+1;
	while (it-z < Today_day) {   
		it = it + 1;
	}
	if ((M_month == Today_month) && (M_year == Today_year) && (M_day == Today_day)) {
		today = document.getElementById(it);
		today.setAttribute("image","chrome://Calendar/content/Images/today.png");
	}
	else {
		today = document.getElementById(it);
		today.style.color='#FF0000';
	}
	if (M_month == 12) {
		var it = h+1;
		while (it-h < 25) {   
			it = it + 1;
		}
		document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/Christmas.png");
	}
	else
		if (M_month == 10) {
			var it = h+1;
			while (it-h < 31) {   
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/hallo.png");
		}
		else
		if (M_month == 2) {
			var it = h+1;
			while (it-h < 14) {  
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/heart.png");
		}
		else
		if (M_month == 4) {
			var it = h+1;
			while (it-h < 1) {   
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/april.png");
		}
		else
		if (M_month == 1) {
			var it = h+1;
			while (it-h < 1) {   
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/fire.png");
		}
		else
		if (M_month == 6) {
			var it = h+1;
			while (it-h < 1) {   
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/party.png");
		}
		else
		if (M_month == 12) {
			var it = h+1;
			while (it-h < 6) {  
				it = it + 1;
			}
			document.getElementById(it).setAttribute("image","chrome://Calendar/content/Images/santa.png");
		}
}

/**
    *@class functions
	*@description The function will load the month view
	*
    *@author Varga Anita
	*@see MonthViewExtract
	*@see MonthViewAdd
*/

function Monthly() {
	var div0 = document.getElementById('divMain000');
	var rows = document.createElement('rows');
	var row = document.createElement('row');
	var button = document.createElement('button');
	button.className="buttonsetup000";
	button.onclick = MonthViewExtract;
	button.setAttribute("image" ,"chrome://Calendar/content/Images/left.png" );
	row.appendChild(button);
	var label = document.createElement('label');
	label.className = "dates";
	row.appendChild(label);	
	var label = document.createElement('label');
	label.className = "dates";
	label.setAttribute("id", "monthvalue");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates";
	label.setAttribute("id", "yearvalue");
	row.appendChild(label);
	var label = document.createElement('label');
	label.className = "dates";
	row.appendChild(label);	
	var button = document.createElement('button');
	button.className="buttonsetup000";
	button.setAttribute("image" ,"chrome://Calendar/content/Images/right.png" );
	button.onclick = MonthViewAdd;
	row.appendChild(button);	
	rows.appendChild(row);
	var row = document.createElement('row');	
	var label = document.createElement('label');
	label.setAttribute("value", "Sunday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Monday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Tuesday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Wednesday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Thursday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Friday");
	label.className = "labelsetup1";
	row.appendChild(label);
	var label = document.createElement('label');
	label.setAttribute("value", "Saturday");
	label.className = "labelsetup1";
	row.appendChild(label);
	rows.appendChild(row);
	div0.appendChild(rows);
	var root = document.getElementById('divMain2');		
	var grid = document.createElement('grid');
	grid.className="scrollable";
	var rows = document.createElement('rows');
	var row = document.createElement('row');
	var columns = document.createElement('columns');	
	for (var i = 0;i<8;i++) {
		var column = document.createElement('column');
		columns.appendChild(column);
	}
	grid.appendChild(columns);
	var it = 0;
	for (var j = 0; j< 5; j++) {
		var row = document.createElement('row');
		for (var z = 0;z<7;z++) {	
			it = it+1;
			var button = document.createElement('button');
			button.className="buttonsetup2";
			button.setAttribute("id", it);
			button.onclick = dontes;
			row.appendChild(button);
		}
		rows.appendChild(row);
    }
	var row = document.createElement('row');
	for (var j = 0; j< 3; j++) {
		it = it+1;
		var button = document.createElement('button');
		button.className="buttonsetup2";	
	    button.setAttribute("id", it);
		button.onclick = dontes;
		row.appendChild(button);
	}
		rows.appendChild(row);
		grid.appendChild(rows);
		root.appendChild(grid);
}

/**
    *@class functions
	*@description The function will load the views
	*
    *@author Varga Anita
	*@see Daily
	*@see Weekly
	*@see Monthly
*/

function Init() {
	Daily();
	Weekly();
	Monthly();
}
